﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Oliving.Edge.Common.Extensions
{
    public static class ListChildExtensions
    {



        /// <summary>
        /// 将list 集合 转换成tree结构的集合
        /// </summary>
        /// <typeparam name="T">对象类类型</typeparam>
        /// <param name="list">元数据</param>
        /// <param name="id">根节点编号【0】</param>
        /// <param name="idName">主键名称（字符串ID）</param>
        /// <param name="pIdName">父节点名称（字符串PID）</param>
        /// <param name="ChildName">子数据类对象</param>
        /// <returns></returns>
        public static List<T> ConvertTreeList<T>(this List<T> list, int id, string idName, string pIdName, string ChildName) where T : new()
        {
            List<T> treeList = new List<T>();

            T model = new T();
            Type type = typeof(T);
            PropertyInfo name = model.GetType().GetProperty(ChildName);

            Type typeList = typeof(List<T>);

            if (typeList.FullName == name.PropertyType.FullName)
            {
                treeList = GetTreeListChild(list, id, idName, pIdName, ChildName);
            }



            return treeList;
        }

        /// <summary>
        /// 将list 集合 转换成tree结构的集合
        /// </summary>
        /// <typeparam name="T">对象类类型</typeparam>
        /// <param name="list">元数据</param>
        /// <param name="id">根节点编号【0】</param>
        /// <param name="idName">主键名称（字符串ID）</param>
        /// <param name="pIdName">父节点名称（字符串PID）</param>
        /// <param name="ChildName">子数据类对象</param>
        /// <returns></returns>
        public static List<T> ConvertTreeList<T>(this List<T> list, string id, string idName, string pIdName, string ChildName) where T : new()
        {
            List<T> treeList = new List<T>();

            T model = new T();
            Type type = typeof(T);
            PropertyInfo name = model.GetType().GetProperty(ChildName);


            Type typeList = typeof(List<T>);

            if (typeList.FullName == name.PropertyType.FullName)
            {
                treeList = GetTreeListChild(list, id, idName, pIdName, ChildName);
            }



            return treeList;
        }




        private static List<T> GetTreeListChild<T>(List<T> list, int id, string mid, string pid, string ChildName) where T : new()
        {
            List<T> nlist = new List<T>();
            List<T> newList = list.FindAll(t => Convert.ToInt32(t.GetType().GetProperty(pid).GetValue(t, null)) == id).ToList();
            foreach (T mm in newList)   //父菜单的子菜单们的递归
            {
                int cid = Convert.ToInt32(mm.GetType().GetProperty(mid).GetValue(mm, null));
                var childList = GetTreeListChild(list, cid, mid, pid, ChildName).ToList();
                var item = mm.GetType();
                item.GetProperty(ChildName).SetValue(mm, childList, null);
                nlist.Add(mm);
            }

            return nlist;
        }

        private static List<T> GetTreeListChild<T>(List<T> list, string
            id, string mid, string pid, string ChildName) where T : new()
        {
            List<T> nlist = new List<T>();
            List<T> newList = list.FindAll(t => (t.GetType().GetProperty(pid).GetValue(t, null)).ToString() == id).ToList();
            foreach (T mm in newList)   //父菜单的子菜单们的递归
            {
                string cid = (mm.GetType().GetProperty(mid).GetValue(mm, null)).ToString();
                var childList = GetTreeListChild(list, cid, mid, pid, ChildName).ToList();

                var item = mm.GetType();
                item.GetProperty(ChildName).SetValue(mm, childList, null);
                nlist.Add(mm);

            }
           
            return nlist;
        }




    }
}
